package our_tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.FileNotFoundException;
import java.io.IOException;

import logic.huffman.HuffmanCode;
import logic.huffman.HuffmanTree;

import org.junit.Test;

import datamodel.RGB;
import datamodel.huffman.tree.Tree;


public class HuffmanCodeTest {
	@Test
	public void testGetHuffmanTree() {
		// build the correct tree and compare (The brightness values are probably wrong, but this is what the
		// ImageIterator shows during debugging. Not my fault...
		Tree shortenedHT = new Tree();
		shortenedHT.setCurrentNode(new Tree.Node(null)); // 64
		
		shortenedHT.setLeftNode(new Tree.Node(null)); // 36
		shortenedHT.setRightNode(new Tree.Node(new RGB(0,255,0,255))); // 28 - green
		shortenedHT.moveToLeftNode();
		
		shortenedHT.setLeftNode(new Tree.Node(null)); // 21
		shortenedHT.setRightNode(new Tree.Node(null)); // 15
		shortenedHT.moveToRightNode();
		shortenedHT.setLeftNode(new Tree.Node(new RGB(255, 0, 0,255))); // 8 - red
		shortenedHT.setRightNode(new Tree.Node(new RGB(0,255,255,255))); // 7 - cyan
		
		shortenedHT.moveToParentNode();
		shortenedHT.moveToLeftNode();
		
		shortenedHT.setLeftNode(new Tree.Node(new RGB(255,0,255,255))); // 12 - purple
		shortenedHT.setRightNode(new Tree.Node(null)); // 9
		
		shortenedHT.moveToRightNode();
		shortenedHT.setLeftNode(new Tree.Node(new RGB(0,0,0,255))); // 6 - black
		shortenedHT.setRightNode(new Tree.Node(new RGB(255, 255, 255, 255))); // 3 - white
				
		
		try {
			Tree actualHT = HuffmanTree.getHuffmanTree("test2.png");
			HuffmanCode hc = new HuffmanCode(actualHT);
			hc.compress(1); // should change actualHT
			assertEquals(shortenedHT, actualHT);
		} catch (FileNotFoundException e) {
			System.err.println("\"test2.png\" not found: ");
			e.printStackTrace();
		} catch (IOException e) {
			System.err.println("some IO-related error occured: ");
			e.printStackTrace();
		}
		
	}
	
	@Test
	public void containsColorTest() {
		Tree shortenedHT = new Tree();
		shortenedHT.setCurrentNode(new Tree.Node(null));
		
		shortenedHT.setLeftNode(new Tree.Node(null));
		
		shortenedHT.setRightNode(new Tree.Node(null));
		shortenedHT.moveToRightNode();
		shortenedHT.setLeftNode(new Tree.Node(new RGB(30,30,30,30)));
		shortenedHT.moveToParentNode();
		
		shortenedHT.moveToLeftNode();
		shortenedHT.setLeftNode(new Tree.Node(null));
		shortenedHT.moveToLeftNode();
		shortenedHT.setRightNode(new Tree.Node(new RGB(2,2,2,2)));
		HuffmanCode hc = new HuffmanCode(shortenedHT);
		assertTrue(hc.containsColor(new RGB(2,2,2,2)));
		assertFalse(hc.containsColor(new RGB(2,2,2,20)));
	}
}
